package com.kingtsoft.study.webserivce.imp;

import javax.servlet.http.HttpSession;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kingtsoft.study.common.ContextHolder;
import com.kingtsoft.study.common.JsonResult;
import com.kingtsoft.study.constant.ParamConstants;
import com.kingtsoft.study.model.StudyStudent;
import com.kingtsoft.study.model.StudyTeacher;
import com.kingtsoft.study.model.StudyUser;
import com.kingtsoft.study.service.StudyUserService;
import com.kingtsoft.study.util.JacksonUtil;
import com.kingtsoft.study.webserivce.WebLoginService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class WebLoginServiceImpl implements WebLoginService {


	@Reference
	private StudyUserService studyUserService;

	/**
	 *
	 * @Title: GetLoginName
	 * @Description: 获取登录名称
	 * @return
	 * @return: String
	 */
	@Override
	public String getLoginName() {
		try {
			StudyUser u = ContextHolder.getSessionUser();
			if (u == null)
				return "";
			if (u.getUserType().equals(ParamConstants.USER_TYPE_0)) {
				return ContextHolder.getSessionStudent().getStudentName();
			} else if (u.getUserType().equals(ParamConstants.USER_TYPE_1)) {
				return ContextHolder.getSessionTeacher().getTeacherName();
			}
			return u.getUsername();
		} catch (Exception e) {
			e.printStackTrace();
			return "Error";
		}
	}

	/**
	 *
	 * @Title: doLogin
	 * @Description: 用户登录校验
	 * @author: shenlx
	 * @param studyUser
	 * @return: void
	 */
	@Override
	public JsonResult doLogin(StudyUser studyUser) {
		String result = studyUserService.doLogin(studyUser);
		JsonResult jr = JacksonUtil.json2Bean(result, JsonResult.class);
		if(null!=jr&&jr.getStatusCode()==ParamConstants.SUCCESS){
			StudyUser u = JacksonUtil.json2Bean(JacksonUtil.getNode(result, "object"), StudyUser.class);
			if (StringUtils.equals(u.getUserType(), ParamConstants.USER_TYPE_0)) {
				System.out.println("这是一个学生===================");

				StudyStudent student = studyUserService.selectStudentByUserId(u.getId());

				ContextHolder.getSession().setAttribute(ParamConstants.LOCAL_CLINET_STUDENT, student);

			} else if (StringUtils.equals(u.getUserType(), ParamConstants.USER_TYPE_1)) {
				System.out.println("这是一个老师===================");

				StudyTeacher teacher = studyUserService.selectTeacherByUserId(u.getId());

				ContextHolder.getSession().setAttribute(ParamConstants.LOCAL_CLINET_TEACHER, teacher);

			} else {
				System.out.println("未指定用户类型===================");
			}

			ContextHolder.getSession().setAttribute(ParamConstants.LOCAL_CLINET_USER, u);
		}
		return jr;
	}

	/**
	 *
	 * @Title: remoteUsername
	 * @Description: 校验用户名唯一性
	 * @author: shenlx
	 * @param username
	 * @return
	 * @return: boolean
	 */
	@Override
	public boolean remoteUsername(String username) {
		return studyUserService.remoteUsername(username);
	}

	/**
	 *
	 * @Title: addRegister
	 * @Description: 用户注册
	 * @author: shenlx
	 * @param studyUser
	 * @param studentName
	 *            这里为了方便，只做了学生注册，并只保存学生姓名
	 * @return
	 */
	@Override
	public void addRegister(StudyUser studyUser, String studentName) {
		studyUserService.addRegister(studyUser, studentName);
	}

	/**
	 * 登出
	 */
	@Override
	public void doLogout() {
		HttpSession session = ContextHolder.getSession();
		if (session != null) {
			session.removeAttribute(ParamConstants.LOCAL_CLINET_USER);
			session.removeAttribute(ParamConstants.LOCAL_CLINET_STUDENT);
			session.removeAttribute(ParamConstants.LOCAL_CLINET_TEACHER);
			session.invalidate();
		}
	}

	/**
	 * 获取菜单
	 * @return
	 * @author jerry.pan
	 * @date 2017-04-12 10:04:33
	 */
	@Override
	public List menu(){
		StudyUser studyUser = ContextHolder.getSessionUser();
		String userType = studyUser.getUserType();
		String menuJson = "";
		if(ParamConstants.USER_TYPE_0.equals(userType)){//学生
			menuJson = "[{\"name\":\"学生菜单\",\"url\":\"\",\"class\":\"glyphicon-education\",\"child\":[{\"name\":\"作业列表\",\"url\":\"/study/homework/page/list?publishStatus=1\",\"class\":\"glyphicon-book\",\"icon\":\"icon-more\"}]}]";
		}else if(ParamConstants.USER_TYPE_1.equals(userType)){//老师
			menuJson = "[{\"name\":\"老师菜单\",\"url\":\"\",\"class\":\"glyphicon-education\",\"child\":[{\"name\":\"我的发布\",\"url\":\"/study/homework/page/findByTeacher\",\"class\":\"glyphicon-book\",\"icon\":\"icon-more\"}]}]";
		}
		List menuList = JacksonUtil.json2Bean(menuJson,ArrayList.class);
		return menuList;
	}
}
